package com.dididoctor.patient.Utils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.dididoctor.patient.Activity.PicScan.PicScanActivity;
import com.dididoctor.patient.MV.adapter.CommonAdapter;
import com.dididoctor.patient.MV.adapter.ViewHolder;
import com.dididoctor.patient.R;
import com.loopj.android.image.SmartImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：PsdApp
 * 类描述：添加多张图片的适配器
 * 创建人：chenjingyao
 * 创建时间：2016/1/5 10:36
 * 修改人：
 * 修改时间：2016/1/5 10:36
 * 修改备注：
 */
public abstract class PictureGridViewAdapter extends CommonAdapter<ImageInfo> {

    private LoaderImage mLoaderImage;
    /**
     * 点击图片是否可以响应点击事件
     */
    private boolean mResponseClick = true;
    private int NoNum=0;

    public PictureGridViewAdapter(Context context) {
        this(context, null);
    }

    public PictureGridViewAdapter(Context context, List<ImageInfo> datas) {
        super(context, datas, R.layout.item_gv_image);
        mLoaderImage = new LoaderImage(context.getApplicationContext(), 256, 256, LoaderImage.chatImageOptions);
    }

    @Override
    public void convert(final int position, ViewHolder helper, ImageInfo item) {
        SmartImageView imageView = helper.getView(R.id.iv_imageContent);
        ImageView iv_delete = helper.getView(R.id.iv_delete);
        if (item.getType() == 0) {
            (imageView).setImageResource(item.getResource());
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addImage();
                }
            });
            iv_delete.setVisibility(View.GONE);
        } else if (item.getType() == 3) {
            iv_delete.setVisibility(View.VISIBLE);
//            mLoaderImage.load(imageView, item.getKey());
            imageView.setImageUrl(item.getUrl(),R.drawable.default_image_onfail, R.drawable.default_image_loading);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    handleExistImage();
                    if (canResponseClick()) {
                        toPictureDetail(position);
                    }
                }
            });
        }
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteImage(position);
            }
        });
    }

    protected abstract void addImage();

    protected abstract void handleExistImage();

    protected abstract void deleteImage(int position);

    /**
     * 返回拼接好的一串图片id
     *
     * @return
     */
    public String getImageKeys() {
        if (getCount() < 1) {
            return "";
        }
        String keys = "";
        for (ImageInfo imageInfo : getData()) {
            if (imageInfo.getType() != 0) {
                if (Util.isEmpty(keys)) {
                    keys = imageInfo.getKey();
                } else {
                    keys += "," + imageInfo.getKey();
                }
            }
        }
        return keys;
    }

    /**
     * 增加一个添加图片的标识"+"
     */
    public void addImageSign() {
        addToFirst(createImageSign());
    }

    /**
     * 给指定位置增加一个添加图片的标识"+"
     */
    public void addImageSign(int position) {
        getData().add(position, createImageSign());
    }

    private ImageInfo createImageSign() {
        ImageInfo imageInfo = new ImageInfo();
        imageInfo.setType(0);
        imageInfo.setResource(R.drawable.btn_add_photo);
        imageInfo.setId(1);

        return imageInfo;
    }

    /**
     * 添加一组图片
     *
     * @param imgArray 图片id数组
     */
    public void addImageArray(String[] imgArray) {
        addData(convertArrayToList(imgArray));
    }

    private List<ImageInfo> convertArrayToList(String[] imgArray) {
        if (imgArray == null || imgArray.length == 0) {
            return null;
        }

        List<ImageInfo> list = new ArrayList<ImageInfo>();
        for (String imgId : imgArray) {
            ImageInfo imageInfo = new ImageInfo();
            imageInfo.setType(3);
            imageInfo.setIds(imgId);

            list.add(imageInfo);
        }

        return list;
    }

    public List<String> toAlist(List<ImageInfo> datas){
        if (datas == null || datas.size() == 0) {
            return null;
        }
        List<String > lString = new ArrayList<String >();
        for (int x=0; x<datas.size();x++){
            if ("3".equals(datas.get(x).getType()+"")){
                lString.add(datas.get(x).getUrl());
            }else{
                NoNum++;
            }
        }
        return lString;
    }

    /**
     * 跳转到图片详情
     *
     * @param position
     */
    private void toPictureDetail(int position) {

        Intent intent = new Intent(context, PicScanActivity.class);
        intent.putStringArrayListExtra("img_urls", (ArrayList<String>) toAlist(datas));
        intent.putExtra("position", position-NoNum);
        if (context instanceof Activity) {
            ActivityAnimationUtils.startNewActivity((Activity) context, intent);
            NoNum = 0;
        }
    }

    protected boolean canResponseClick() {
        return mResponseClick;
    }

    /**
     * 设置图片是否可以响应点击事件
     *
     * @param canResponseClick true允许图片响应点击事件，false表示不允许
     */
    public void setResponseClick(boolean canResponseClick) {
        mResponseClick = canResponseClick;
    }
}
